package kr.hyosang.dkbs.kiosk;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class Env {
	public static String SERVER_HOST;
	
	
	public static String getRootPath() {
		String rootPath;
		if(File.separatorChar == '/') {
			//Linux
			rootPath = "/kiosk";
		}else {
			//Windows
			rootPath = "C:\\kiosk";
		}
		
		return rootPath;
		
	}
	public static boolean load() {
		String confPath = getRootPath();
		
		File confFile = new File(confPath + "/kiosk.config");
		
		if(!confFile.exists()) {
			try {
				InputStream is = Env.class.getClassLoader().getResourceAsStream("default.config");
				
				confFile.getParentFile().mkdirs();
				confFile.createNewFile();
				FileOutputStream fos = new FileOutputStream(confFile);
				
				byte [] buf = new byte[1024];
				int nRead;
				
				while( (nRead = is.read(buf) ) > 0) {
					fos.write(buf, 0, nRead);
				}
				
				fos.close();
				is.close();
			}catch(IOException e) {
				e.printStackTrace();
				return false;
			}
		}
		
		Log.log("Loads config from " + confFile.getAbsolutePath());
		
		try {
			Properties prop = new Properties();
			prop.load(new FileInputStream(confFile));
			
			SERVER_HOST = prop.getProperty("server.host");
		}catch(IOException e) {
			e.printStackTrace();
		}
		
		Log.log("SERVER_HOST : " + SERVER_HOST);
		
		return true;
	}
	

}
